<template>
	<view>
		<view class="upload-imgs">
			<view style="position: relative;" v-for="(item, index) in value" :key="index">
				<image :src="item | formatFile" class="image" :style="{width: imageWH + 'rpx', height: imageWH + 'rpx'}"></image>
				<image src="/static/images/icon-del.png" mode="" class="del-icon" @tap="delImg(index)"></image>
			</view>
			<view class="icon-upload" @tap="uploadImage" v-if="value.length < limit"  :style="{width: imageWH + 'rpx', height: imageWH + 'rpx'}">
				<image src="../../static/images/icon-upload.png" mode=""></image>
				<text>添加物品图片</text>
			</view>
			
		</view>
	</view>
</template>

<script>
	import { uploadImage } from '@/utils/upload.js'
	export default {
		name: "my-upload-image",
		props:{
			value: {
				default: []
			},
			imageWH: {	// 图片宽高
				default: 180
			},
			limit: {	// 可上传图片数量
				default: 5
			}
		},
		data() {
			return {
				
			};
		},
		methods:{
			uploadImage(){
				uploadImage(this.limit - this.value.length).then(res => {
					if(res.length > 1){
						res.forEach(item => {
							this.value.push(item.fileName)
						})
					}else{
						this.value.push(res.fileName)
					}
					
					this.$emit('input', this.value)
				})
			},
			delImg(index){
				this.value.splice(index,1)
				this.$emit('input', this.value)
			}
		}
	}
</script>

<style lang="scss">
	.upload-imgs {
		display: flex;
		flex-wrap: wrap;

		.image, 
		.icon-upload {
			margin-bottom: 20rpx;
			margin-right: 30rpx;
			border-radius: 20rpx;
		}
		
		.icon-upload{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			background-color: #F8F8F8;
			
			image{
				width: 56rpx;
				height: 56rpx;
				margin-bottom: 25rpx;
			}
			
			text{
				font-size: 26rpx;
				font-weight: bold;
				color: #999999;
			}
		}

		.del-icon {
			position: absolute;
			top: -10rpx;
			right: 15rpx;
			width: 40rpx;
			height: 40rpx;
			margin: 0;
			z-index: 999;
		}
	}
</style>